<?php

use Carbon\Carbon;

/* * ****************************************************************************
 * controllers\CalendarController
 * 
 * author  Laureta Xhaferaj
 * email   laureta@first.al
 * 
 * @function index
 * @function create
 * @function store
 * @function show
 * @function edit
 * @function update
 * @function destroy
 * *************************************************************************** */

class CalendarController extends \BaseController {

    /**
     * Display the calendar values.
     *
     * @return Response
     */
    public function index() {
        $data = array();
        $data2 = array();
        $nrCheckOut = 0;
        $nrCheckIn = 0;
        $nrRezerv = 0;
        $nrRezerv1 = 0;
        $nrRezervPaguar = 0;
        $style = '';
        $desc = '';
        $selectCars = '';
        $result = DB::select("SELECT * FROM cars ");
        $num = count($result);
        date_default_timezone_set('Europe/Rome');

        $i = 0;
        while ($i < $num) {
            $first = $result[$i]->Description;
            $idCar = $result[$i]->id; // ver nje kusht qe viti aktual kur eshte mars dhe viti perpara kur eshte janar
            if (date("m") > 2) {
                $yearToday = date("Y");
                $monthToday = date("m") - 1;
            } else {
                $yearToday = date("Y") - 1;
                $monthToday = date("m") + 11;
            }
            $today = "" . $yearToday . "-" . $monthToday . "-" . "1";


            $result2 = DB::select("Select r.*,r.drpasportid as pasId,r.status as st,c.* from rentals r left join cars c on c.id=r.vehicle "
                . "where c.id=" . $idCar . "  and STARTTIME>='" . $today . "'");
            $num2 = count($result2);
            $selectCars = $selectCars . "<option>" . $first . "</option>";
            $i2 = 0;
            if ($num2 == 0) {
                $data2[] = array(
                    'label' => $idCar . "\n" . '' . $first
                );
            }
            $once = true;


            while ($i2 < $num2) {
                $start = $result2[$i2]->STARTTIME;
                $end = $result2[$i2]->ENDTIME;
                $nowDate = date("Y-m-d h:m:s");

                $idCusotmer = $result2[$i2]->drpasportid;
                $status = $result2[$i2]->st;
                $rezervationPayed = $result2[$i2]->rezervationPayed;
                $rezervationAmount = $result2[$i2]->rezervationAmount;
                $name = $result2[$i2]->drfirstname;
                $surname = $result2[$i2]->drlastname;
                $email = $result2[$i2]->dremail;
                $tel = $result2[$i2]->drhphone;
                $rentDays = $result2[$i2]->rentdays;
                $agents = $result2[$i2]->agent;

                $today_given = DB::select('select * from rentals where STARTTIME >= "'.$nowDate.'" AND ENDTIME > "'.$nowDate.'" and status="CHECK_OUT" ');
                $today_reserved = DB::select('select * from rentals where STARTTIME >= "'.$nowDate.'" AND ENDTIME > "'.$nowDate.'" and status="REZERVATION" ');
                $today_back = DB::select('select * from rentals where ENDTIME <= "'.$nowDate.'" and status="CHECK_IN" ');
                $delayed_back = DB::select('select * from rentals where ENDTIME <= "'.$nowDate.'" and status="CHECK_OUT" ');
                $delayed_reservation = DB::select('select * from rentals where ENDTIME <= "'.$nowDate.'" and status="REZERVATION" ');
                $in_use = DB::select('select * from rentals where STARTTIME <= "'.$nowDate.'" AND ENDTIME > "'.$nowDate.'" and status="CHECK_OUT" ');
                $in_use = DB::select('select * from rentals where STARTTIME <= "'.$nowDate.'" AND ENDTIME > "'.$nowDate.'" and status="REZERVATION" ');
                $back = DB::select('select * from rentals where STARTTIME < "'.$nowDate.'" AND ENDTIME < "'.$nowDate.'" and status="CHECK_IN" ');


               // $result3 = DB::select("Select * from customers where drpasportid='" . $idCusotmer . "'");
               // $num3 = count($result3);

//                $name = $result3[0]->drfirstname;
//                $surname = $result3[0]->drlastname;
//                $email = $result3[0]->DREMAIL;
//                $tel = $result3[0]->drhphone;


                $from = date('Y-m-d', strtotime($start));
                $to = date('Y-m-d', strtotime($end));

                $style;


                $start1 = strtotime($nowDate);
                $end1 = strtotime($end);
                $days1 = $start1 - $end1;
                $days2 = ceil($days1 / 86400);

                $nowYear = date("Y");
                $nowDay = date("d");
                $nowMonth = date("m");

                $startYear = date("Y", strtotime($start));
                $startDay = date("d", strtotime($start));
                $startMonth = date("m", strtotime($start));

                $endYear = date("Y", strtotime($end));
                $endDay = date("d", strtotime($end));
                $endMonth = date("m", strtotime($end));

                if ($i2 == $num2 - 1) {
                    $once = false;
                }
                if ($status == "CHECK_OUT" && $once == false) {
                    $nrCheckOut++;
                }
                if ($status == "CHECK_IN" && $once == false) {
                    $nrCheckIn++;

                }if ($nowYear == $endYear && $nowDay == $endDay && $nowMonth == $endMonth) {

                    $style = "kthehensot";
                    $desc = Lang::get('calendar.returned_today');
                } else if ($nowYear == $startYear && $nowDay == $startDay && $nowMonth == $startMonth) {

                    $style = "dhenesot";
                    $desc = Lang::get('calendar.given_today');
                } else if (strtotime($end) <= strtotime($nowDate) && $status == "CHECK_OUT") {
                    $style = "vonuar";
                    $desc = Lang::get('calendar.delayed');
                } else if (strtotime($start) <= strtotime($nowDate) && $agents != 0) {

                    $result4 = DB::select("Select * from agents where ID='" . $agents . "'");
                    $agname = $result4[0]->COMPANYNAME;
                    $style = "agjensi$agname";
                    $desc = Lang::get('calendar.agensy');
                } else if (strtotime($start) <= strtotime($nowDate) && $status != "CHECK_IN") {
                    $style = "perdorim";
                    $desc = Lang::get('calendar.in_use');
                } else if ($status == "REZERVATION" && $start > $nowDate) {


                    if ($rezervationPayed == 1) {

                        if ($once == false)
                            $nrRezervPaguar++;

                        $style = "rezervuarpaguar";
                        $desc = Lang::get('calendar.reserved_payed');
                    }else {
                        if ($once == false and $name == 'Servis') {
                            $nrRezerv1++;
                            $style = "servis";
                        } elseif ($rezervationPayed == 0) {
                            $nrRezerv++;
                            $style = "rezervuar";
                            $desc = Lang::get('calendar.reserved');
                        }
                    }
                } else if (strtotime($start) > strtotime($nowDate) && $status == "CHECK_OUT") {
                    $style = "check_out";
                    $desc = Lang::get('calendar.check_out');
                }else {
                    $style = "kthyer";
                    $desc = Lang::get('calendar.returned');
                }
                $once = true;

                $data[] = array(
                    'label' => $idCar . "\n" . '' . $first,
                    'start' => $from,
                    'end' => $to,
                    'desc' => $desc . "\n" . '' . $first . "\n" . '' . $name .
                        ' ' . $surname . "\n" . $email . "\n" . $tel . "\n" .
                        Lang::get('calendar.start') . " > "
                        . $start . "\n" . Lang::get('calendar.end') . "> " . $end . "\n" .
                        Lang::get('calendar.nr_days') . $rentDays,
                    'class' => $style
                );
                $i2++;
            }
            $i++;
        }
        $toDayDate = date("Y-m-d");
        $toDayDate2 = date("Y-m-d");
        $toDayDate = $toDayDate . " 00:00:00";
        $toDayDate2 = $toDayDate2 . " 23:59:59";

        return View::make('pages.booking', array(
            'toDayDate' => $toDayDate,
            'toDayDate2' => $toDayDate2,
            'data' => $data,
            'data2' => $data2
        ));
    }


}
